﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using Microsoft.Ccr.Core;

namespace XcoAppSpaces.Dialogs
{
    public class StatemachineDialog<TStates> : Dialog
    {
        static StatemachineDialog()
        {
            if (!typeof(TStates).IsEnum)
                throw new ArgumentException(string.Format("Statemachines can only be defined for enumerable types! Generic parameter {0} is not an enumerable type, though.", typeof (TStates).FullName));
        }


        public StatemachineDialog(){}
        public StatemachineDialog(TStates startState)
        {
            this.State = startState;    
        }


        public TStates State { get; set; }


        public void Transition<TTriggerMessage>(TStates currentState, Handler<TTriggerMessage> handler) { Transition(currentState, handler, false); }
        public void Transition<TTriggerMessage>(TStates currentState, Handler<TTriggerMessage> handler, bool handleInSyncContext)
        {
            this.handlers.Add(new DialogMessageHandler<TTriggerMessage>(handler, 
                                                                        handleInSyncContext ? this.syncContext : null,
                                                                        m => this.State.Equals(currentState)));
        }

        public StatemachineDialogMessageHandler<TTriggerMessage, TStates> Transition<TTriggerMessage>()
        {
            var dmh = new StatemachineDialogMessageHandler<TTriggerMessage, TStates>(this, this.syncContext);
            this.handlers.Add(dmh);
            return dmh;
        }
    }
}
